package com.kd.crawler.common.jms;

import javax.jms.Connection;
import javax.jms.DeliveryMode;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.management.MBeanServerConnection;
import javax.management.MBeanServerInvocationHandler;
import javax.management.ObjectName;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;

import org.apache.activemq.broker.jmx.BrokerViewMBean;
import org.apache.activemq.broker.jmx.QueueViewMBean;

/**
 * 消息的生产者（发送者）
 * 
 * @author liang
 *
 */
public class ActiveMQProducer {

	
	public static void sendTopicMessage(String topicName, String message){
		sendMessage(1,topicName, message, Session.AUTO_ACKNOWLEDGE);
	}
	
	/**
	 * 发送队列消息
	 * @param queueName
	 * @param message
	 */
	public static void sendQueueMessage(String messageName, String message) {
		sendMessage(0,messageName, message, Session.AUTO_ACKNOWLEDGE);
	}
	/**
	 * 	发送队列信息
	 * @param messageType 0：Queue消息，1：topic消息
	 * @param messageName
	 * @param clientID
	 * @param message
	 * @param acknowledgeMode
	 */
	public static void sendMessage(int messageType,String messageName, String message,int acknowledgeMode) {
		// 连接
		Connection connection = null;
		// 会话 接受或者发送消息的线程
		Session session=null;
		try {
			connection = MQPooledConnectionFactory.getConnection();
			// 启动连接
			connection.start();
			// 创建session
			session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);
			Destination destination=null;
			switch(messageType){
				case 0:
					destination = session.createQueue(messageName);
					break;
				case 1:
					destination = session.createTopic(messageName);
					break;
			}
			// 消息生产者
			MessageProducer messageProducer = session.createProducer(destination);
			messageProducer.setDeliveryMode(DeliveryMode.PERSISTENT);
			// 发送消息
			sendMessage(session, messageProducer,message);
			session.commit();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if(session!=null){
				try {
					session.close();
				} catch (JMSException e) {
					e.printStackTrace();
				}
			}
			if (connection != null) {
				try {
					connection.close();
				} catch (JMSException e) {
					e.printStackTrace();
				}
			}
		}

	}		
	
	/**
	 * 发送消息
	 * 
	 * @param session
	 * @param messageProducer
	 *            消息生产者
	 * @throws Exception
	 */
	public static void sendMessage(Session session,	MessageProducer messageProducer,String message) throws Exception {
		TextMessage textMessage = session.createTextMessage(message);		
		// 通过消息生产者发出消息
		messageProducer.send(textMessage);
		textMessage.acknowledge();
	}
	
	public static void getQueueSize() throws Exception{
		
		String url = "service:jmx:rmi:///jndi/rmi://113.107.235.90:8161/jmxrmi";
		JMXServiceURL urls = new JMXServiceURL(url);
		JMXConnector connector = JMXConnectorFactory.connect(urls,null);
		connector.connect();
		MBeanServerConnection conn = connector.getMBeanServerConnection();
		//这里brokerName的b要小些，大写会报错
		ObjectName name = new ObjectName("myDomain:brokerName=broker,type=Broker");
		BrokerViewMBean mBean = (BrokerViewMBean)MBeanServerInvocationHandler.newProxyInstance(conn, name, BrokerViewMBean.class, true);
		for(ObjectName na : mBean.getQueues()){
			QueueViewMBean queueBean = (QueueViewMBean) 
			MBeanServerInvocationHandler.newProxyInstance(conn, na, QueueViewMBean.class, true);
			System.out.println("******************************");
			System.out.println("队列的名称："+queueBean.getName());
			System.out.println("队列中剩余的消息数："+queueBean.getQueueSize());
			System.out.println("消费者数："+queueBean.getConsumerCount());
			System.out.println("出队列的数量："+queueBean.getDequeueCount());
		}
	}
	
	public static void main(String args[]) throws Exception{
		ActiveMQProducer.getQueueSize();
	}
	
}